package com.yidong;

import com.YDCommonUtils;
import com.yidong.entity.YiDongObj;
import com.NumberToChinese;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

public class ShouTuoDaiXiao extends YDCommonUtils {

    public static String inPath = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\合同模板\\";
    public static String outPath = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\";

    //受托代销无锡
    public void wuXi(){
        ShouTuoDaiXiao stdx = new ShouTuoDaiXiao();
        YiDongObj obj = new YiDongObj();
        obj = this.shoutuoCaozuo(obj);
        stdx.wordUtils(obj,inPath,outPath,"受托代销无锡.docx","后向");
    }

    //受托代销集成
    public void jicheng(){
        ShouTuoDaiXiao stdx = new ShouTuoDaiXiao();
        YiDongObj obj = new YiDongObj();
        obj = this.shoutuoCaozuo(obj);
        stdx.wordUtils(obj,inPath,outPath,"受托代销集成.docx","后向");
    }

    //处理受过代销 13% 和 6% 的金额
    public YiDongObj shoutuoCaozuo(YiDongObj obj){
        String shebei = obj.getStSbf(); // 设备费
        String shouxu = obj.getStSxf(); // 手续费

        // ------ 设备费 ------
        BigDecimal sbf = new BigDecimal(shebei);
        // 计算 b = a / 1.13
        BigDecimal sbCount = new BigDecimal("1.13");
        BigDecimal sbBhs = sbf.divide(sbCount, 2, RoundingMode.HALF_UP);
        // 计算 c = a - b
        BigDecimal sbSe = sbf.subtract(sbBhs);
        // 将结果转换为字符串
        String sbBhsStr = sbBhs.setScale(2, RoundingMode.HALF_UP).toPlainString(); // 确保输出格式为两位小数
        String sbSeStr = sbSe.setScale(2, RoundingMode.HALF_UP).toPlainString(); // 同样确保格式
        obj.setStSbfBhs(sbBhsStr);
        obj.setStSbfSe(sbSeStr);

        // ------ 手续费 ------
        BigDecimal sxf = new BigDecimal(shouxu);
        // 计算 b = a / 1.13
        BigDecimal sxCount = new BigDecimal("1.06");
        BigDecimal sxBhs = sxf.divide(sxCount, 2, RoundingMode.HALF_UP);
        // 计算 c = a - b
        BigDecimal sxSe = sxf.subtract(sxBhs);
        // 将结果转换为字符串
        String sxBhsStr = sxBhs.setScale(2, RoundingMode.HALF_UP).toPlainString(); // 确保输出格式为两位小数
        String sxSeStr = sxSe.setScale(2, RoundingMode.HALF_UP).toPlainString(); // 同样确保格式
        obj.setStSxfBhs(sxBhsStr);
        obj.setStSxfSe(sxSeStr);

        //大写
        NumberToChinese nb = new NumberToChinese();
        obj.setStSbfDx(nb.numberToChinese(shebei));
        obj.setStSxfDx(nb.numberToChinese(shouxu));
        return obj;
    }

    @Override
    public String changeText(String runText, YiDongObj obj) {
        //后向合同
        runText = runText.replaceAll("易项目名称易",obj.getXmmc());
        runText = runText.replaceAll("易前向单位易",obj.getSthtqxdw());
        runText = runText.replaceAll("易后向单位易",obj.getSththxdw());
        runText = runText.replaceAll("易合同期限易",obj.getSthtqx());

        runText = runText.replaceAll("易手续费易",obj.getStSxf());
        runText = runText.replaceAll("易手续费不含税易",obj.getStSxfBhs());
        runText = runText.replaceAll("易手续费税额易",obj.getStSxfSe());
        runText = runText.replaceAll("易手续费大写易",obj.getStSxfDx());

        runText = runText.replaceAll("易受托代销费易",obj.getStSbf());
        runText = runText.replaceAll("易受托代销费不含税易",obj.getStSbfBhs());
        runText = runText.replaceAll("易受托代销费税额易",obj.getStSbfSe());
        runText = runText.replaceAll("易受托代销费大写易",obj.getStSbfDx());

        return runText;
    }

    public static void main(String[] args) {
        // 当前类所在位置的路径
        String currentClassPath = Objects.requireNonNull(ShouTuoDaiXiao.class.getResource("")).getPath();
        System.out.println(currentClassPath);
    }

}
